    //
//  MyTabBarController.m
//  CustomTabbar
//
//  Created by simon on 10-9-15.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "MyTabBarController.h"
#import "MyTabBar.h"
#import "MyTabBarItem.h"
#import "DefaultViewController.h"

@implementation MyTabBarController
@synthesize tabBar, viewControllers, defaultController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
		self.view.frame = CGRectMake(viewX, viewY, viewWidth, viewHeight);		
		
		tabBar = [[MyTabBar alloc] initWithFrame:CGRectMake(tabBarX, tabBarY, tabBarWidth, tabBarHeight)];
		tabBar.leftMargin = tabBarLeftMargin;
		tabBar.rightMargin = tabBarRightMargin;
		tabBar.topMargin = tabBarTopMargin;
		tabBar.bottomMargin = tabBarBottomMargin;
		tabBar.itemSpacing = tabBarItemSpacing;
		tabBar.imageRatio = tabBarImageRatio;
		tabBar.titleFont = tabBarTitleFont;
		tabBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabBar_bg.png"]];
		tabBar.tabBarController = self;
		[self.view addSubview:tabBar];
		
		contentViews = [[NSMutableArray alloc] init];
    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)dealloc {
	[defaultController release];
	[viewControllers release];
	[tabBar release];
	[contentViews release];
    [super dealloc];
}



-(void) setDefaultController:(DefaultViewController *)defaultControllerPara{
	defaultControllerPara.view.frame = CGRectMake(contentViewX, contentViewY, contentViewWidth, contentViewHeight);
	[self.view addSubview:defaultControllerPara.view];

    defaultController = [defaultControllerPara retain];
	defaultController.myTabBarController = self;
}


-(void) setViewControllers:(NSArray *)viewControllersPara{
	viewControllers = [viewControllersPara retain];
	
	NSMutableArray* itemArr = [[NSMutableArray alloc] init];
    
	for(int i=0; i<[viewControllers count]; i++){		
		//取出每个viewController,并且把它和tabBar联系起来
		MyViewController* viewController = (MyViewController*)[viewControllers objectAtIndex:i];
		viewController.tabBarController = self;		
		MyTabBarItem* item = viewController.tabBarItem;				
		item.viewController = viewController;	

		[itemArr addObject:item];
	}
	
	self.tabBar.items = itemArr;
}


-(void) setContentView:(UIView*)contentView{
	contentView.frame = CGRectMake(contentViewX, contentViewY, contentViewWidth, contentViewHeight);
	BOOL exist = NO;
	for (UIView* subview in self.view.subviews) {
		if(subview == contentView){
			exist = YES;
			break;
		}
	}
	if(exist == YES){
		[self.view bringSubviewToFront:contentView];
	}else {
		[self.view addSubview:contentView];
		[contentViews addObject:contentView];
	}

}


-(void) selectControllerAtIndex:(int)index{
	[tabBar setSelectedAtIndex:index];
	MyViewController* viewController = (MyViewController*)[viewControllers objectAtIndex:index];
	[self setContentView: viewController.view];
}

@end
